// MAttitudeWidget.cpp: Implementierung der Klasse MAttitudeWidget.
//
//////////////////////////////////////////////////////////////////////

#include "MAttitudeWidget.h"
//#include "MiriamColors.h"

#include "MProgressBar.h"
#include <iostream>
//#include <math.h>
//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
///some default colours
// const QColor StandardRedFill( 165, 0, 33, 255 );//
// const QColor StandardBlueFill( 51, 51, 153, 255 );//
// 
// const QColor StandardTextColor(Qt::yellow);//
// 
// const QColor MiriamBackgroundColor( 0, 51, 0, 255 );//the dark green background
// const QColor MiriamMiddlegroundColor( 0, 102, 0, 255 );//the mid green used for some panels
// const QColor MiriamBorderColor(0, 255, 0, 255);//the bright green borders

MAttitudeWidget::MAttitudeWidget(QWidget * parent ):MiriamDial01(parent)
{
	lblTitle = 0;
	lblValue = 0;
	bar = 0;
    m_middleColor = QColor( 0, 102, 0, 255 );
    m_textColor = QColor(Qt::yellow);
    m_backgroundColor = QColor( 0, 51, 0, 255 );
    m_markerColor = QColor( 165, 0, 33, 255 );
    
	setAutoFillBackground ( true );
	QPalette p; 
	p.setColor ( QPalette::Window, m_backgroundColor);
	setPalette ( p);

	m_labelWidth = 50;
	m_labelHeight = 20;
	//m_barDim = 20;//this can be width or height depending on the orientation
    
	//make the title label
	lblTitle = new QLabel(this);
    lblTitle->setFixedSize(m_labelWidth, m_labelHeight);
	lblTitle->setAutoFillBackground ( true );
	p.setColor ( QPalette::Window, m_middleColor);
	p.setColor ( QPalette::WindowText, m_textColor);
	lblTitle->setPalette ( p);
	lblTitle->setFrameStyle ( QFrame::Box | QFrame::Plain);
	lblTitle->move(0,0);//it's already at (0,0)
	lblTitle->setAlignment ( Qt::AlignHCenter );
	lblTitle->setMargin ( 0 );
	lblTitle->show();
	lblTitle->setText("title");



	lblTitle->setObjectName("lblTitle"); 
	lblTitle->setFrameStyle (QFrame::StyledPanel);
	lblTitle->setStyleSheet(QString("#lblTitle{"
		"border-width: 1px;"
		"border-style: solid;"
		"border-color: rgb(0, 255, 0);"
	"}")
	);

	
	//make the value label
	lblValue = new QLabel(this);
    lblValue->setFixedSize(m_labelWidth, m_labelHeight);//lblTitle->width(), lblTitle->height());
	lblValue->setAutoFillBackground ( true );
	p.setColor ( QPalette::Window, m_backgroundColor);
	lblValue->setPalette ( p );
	lblValue->setFrameStyle ( QFrame::Box | QFrame::Plain);
	lblValue->move(0, lblTitle->height()-1);//directly below the title
	lblValue->setAlignment ( Qt::AlignHCenter );
	lblValue->setMargin ( 0 );
	lblValue->show();
	lblValue->setText("value");

	lblValue->setObjectName("lblValue"); 
	lblValue->setFrameStyle (QFrame::StyledPanel);
	lblValue->setStyleSheet(QString("#lblValue{"
		"border-width: 1px;"
		"border-style: solid;"
		"border-color: rgb(0, 255, 0);"
	"}")
	);

	//make the filled bar
	bar = new MProgressBar(this);
	bar->setOrientation ( Qt::Vertical );
	bar->setFixedSize ( 20, lblTitle->height()+lblValue->height()-1 );//w, h
	bar->setTextVisible ( false );
	bar->setMinimum ( 0 );
	bar->setMaximum ( 100 );
	bar->setSafeMinimum(10);
	bar->setSafeMaximum(90);
	bar->setValue ( 0 );
    bar->setFrameShape(QFrame::Box);
	bar->setAutoFillBackground ( true );
	p.setColor ( QPalette::Window, m_backgroundColor);
	p.setColor ( QPalette::WindowText, m_textColor);
    bar->setPalette ( p);
	bar->move(lblTitle->width()-1, 0);//glue it on there
	bar->show();

	bar->setObjectName("bar"); 
	bar->setFrameStyle (QFrame::StyledPanel);
	bar->setStyleSheet(QString("#bar{"
		"border-width: 1px;"
		"border-style: solid;"
		"border-color: rgb(0, 255, 0);"
	"}")
	);

	setFixedSize(lblTitle->width()+bar->width() , lblTitle->height()+lblValue->height());
}

MAttitudeWidget::~MAttitudeWidget()
{

}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
///want to keep everything central and make sure the dial resize is called - otherwise, the mask don't get drun
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void MAttitudeWidget::resizeEvent(QResizeEvent * event)
{
	int xCentre = (lblTitle->width() + bar->width())/2;
	int yCentre = bar->height()/2;
	int halfWidth = (lblTitle->width()+bar->width())/2;

	lblTitle->move(width()/2 - halfWidth, height()/2-lblTitle->height());
	lblValue->move(lblTitle->pos() + QPoint(0, lblTitle->height()-1));//directly below the title
	bar->move(lblTitle->pos() + QPoint(lblTitle->width()-1, 0));//glue it on there

	MiriamDial01::resizeEvent(event);
}
void MAttitudeWidget::setTitle(const QString title)
{
	lblTitle->setText(title);
}
void MAttitudeWidget::setValue(const double value)
{
	lblValue->setText(QString("%1 %2").arg(value, 3, 'f', 1).arg(m_suffix));
	bar->setValue(value);
	MiriamDial01::update();
}
double MAttitudeWidget::value()
{
	return bar->value();
}
void MAttitudeWidget::setMinimum(const double value)
{
	bar->setMinimum (value);
}
void MAttitudeWidget::setMaximum(const double value)
{
	bar->setMaximum (value);
}
double MAttitudeWidget::minimum()const
{
	return bar->minimum ();
}
double MAttitudeWidget::maximum()const 
{
	return bar->maximum ();
}
void MAttitudeWidget::setSafeMinimum(const double value)
{
	bar->setSafeMinimum (value);
}
void MAttitudeWidget::setSafeMaximum(const double value)
{
	bar->setSafeMaximum (value);
}

/*!
    \fn MAttitudeWidget::backgroundColor() const
 */
const QColor & MAttitudeWidget::backgroundColor() const
{
    return m_backgroundColor;
}


/*!
    \fn MAttitudeWidget::middleColor() const
 */
const QColor & MAttitudeWidget::middleColor() const
{
    return m_middleColor;
}


/*!
    \fn MAttitudeWidget::textColor() const
 */
const QColor & MAttitudeWidget::textColor() const
{
    return m_textColor;
}


/*!
    \fn MAttitudeWidget::suffix() const
 */
const QString & MAttitudeWidget::suffix() const
{
    return m_suffix;
}


/*!
    \fn MAttitudeWidget::labelHeight() const
 */
int MAttitudeWidget::labelHeight() const
{
    return m_labelHeight;
}


/*!
    \fn MAttitudeWidget::labelWidth() const
 */
int MAttitudeWidget::labelWidth() const
{
    return m_labelWidth;
}


/*!
    \fn MAttitudeWidget::setBarDim(int dim)
 */
void MAttitudeWidget::setBarDim(int dim)
{
    bar->setFixedSize ( dim, lblTitle->height()+lblValue->height()-1 );
}




/*!
    \fn MAttitudeWidget::setLabelHeight(int height)
 */
void MAttitudeWidget::setLabelHeight(int height)
{
    lblTitle->resize(lblTitle->width(),height);
}


/*!
    \fn MAttitudeWidget::setLabelWidth(int width)
 */
void MAttitudeWidget::setLabelWidth(int width)
{
    lblTitle->resize(width,lblTitle->height());
}



/*!
    \fn MAttitudeWidget::setTextColor(QColor textColor)
 */
void MAttitudeWidget::setTextColor(QColor textColor)
{
    m_textColor = textColor;
}



/*!
    \fn MAttitudeWidget::title()
 */
QString MAttitudeWidget::title()
{
    return lblTitle->text();
}


/*!
    \fn MAttitudeWidget::barDim()
 */
int MAttitudeWidget::barDim()
{
    return qMax(bar->width(),bar->height());
}


/*!
    \fn MAttitudeWidget::safeMinimum()
 */
double MAttitudeWidget::safeMinimum()
{
    bar->minSafe();
}


/*!
    \fn MAttitudeWidget::safeMaximum()
 */
double MAttitudeWidget::safeMaximum()
{
    bar->maxSafe();
}
